package y24.m5.file;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class InsertStudent {
    private File file;
    private List<Student> list;

    public InsertStudent(File file, List<Student> list) {
        this.file = file;
        this.list = list;
    }

    public Boolean inputFile() {
        try (FileWriter fileWriter = new FileWriter(this.file)) {
             return writer(fileWriter);
        }catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }


    public Boolean appFile(){
        try (FileWriter fileWriter = new FileWriter(this.file, true)) {
            return writer(fileWriter);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    private Boolean writer(FileWriter fileWriter) throws IOException {
        for (Student student : this.list) {
            fileWriter.write(student.getId() + " " + student.getName() + " " + student.getScore() + "\n");
        }
        return true;
    }

}
